package com.task.scheduler.service;

import com.task.scheduler.enums.TaskExecutionMode;
import com.task.scheduler.model.GenericTask;
import com.task.scheduler.queue.LocalTaskQueueService;
import com.task.scheduler.queue.RedissonTaskQueueService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

@Service
@Primary
@RequiredArgsConstructor
public class HybridTaskService implements TaskService {

    private final LocalTaskQueueService localTaskQueueService;
    private final RedissonTaskQueueService redissonTaskQueueService;

    @Override
    public void submit(GenericTask task) {
        if (isLocal(task)) {
            localTaskQueueService.submit(task);
        } else {
            redissonTaskQueueService.submit(task);
        }
    }

    @Override
    public void cancel(String taskId) {
        localTaskQueueService.cancel(taskId);
        redissonTaskQueueService.cancel(taskId);
    }

    private boolean isLocal(GenericTask task) {
        return task.getExecutionMode() == TaskExecutionMode.LOCAL;
    }
}
